package com.yt.demo.controller;

import com.yt.demo.dto.BaseReq;
import com.yt.demo.dto.BaseResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {

    @GetMapping("/testGet")
    public BaseResp<String> testGet(@RequestParam("param") String param) {
//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//        if(!param.isEmpty()){
//            throw new RuntimeException("error...");
//        }
        log.info("[demo-hello] testGet:{}", param);
        return BaseResp.success(param);
    }

    @PostMapping("/testPost")
    public BaseResp<String> testPost(@RequestBody BaseReq<String> req) {
        log.info("[demo-hello] testPost:{}", req);
        return BaseResp.success(req.getParam());
    }

    @PostMapping("/testFile")
    public BaseResp<String> testFile(@RequestParam("file") MultipartFile multipartFile) {
        return BaseResp.success("接收到文件: " + multipartFile.getOriginalFilename());
    }

    @GetMapping("/testRibbon")
    public BaseResp<String> testRibbon(@RequestParam("param") String param, HttpServletRequest httpServletRequest) {
        log.info("接收到请求，参数:{}, serverPort:{}", param, httpServletRequest.getServerPort());
        return BaseResp.success("Response from server port: " + httpServletRequest.getServerPort());
    }
}
